package com.ysd.lis.service.lab.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ysd.common.Result;
import com.ysd.lis.entity.lab.LabReqState;
import com.ysd.lis.mapper.lab.LabReqStateMapper;
import com.ysd.lis.request.DeleteParam;
import com.ysd.lis.service.lab.LabReqStateService;
import com.ysd.util.ToolsUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * LAB_REQ_MAIN状态表 服务实现类
 * </p>
 *
 * @author Bai
 * @since 2024-01-16
 */
@Service
public class LabReqStateServiceImpl extends ServiceImpl<LabReqStateMapper, LabReqState> implements LabReqStateService {

    @Autowired
    LabReqStateMapper labReqStateMapper;


    @Override
    public Result deleteReqState(DeleteParam param) {
        if (ToolsUtils.isEmpty(param)) {
            return Result.fail("入参不可为空！");
        }
        if (ToolsUtils.isEmpty(param.getIds())) {
            return Result.fail("入参不可为空！");
        }
        String ids = param.getIds();
        String[] array = ids.split(",");
        for (int i = 0; i < array.length; i++) {
            LambdaUpdateWrapper<LabReqState> lambdaUpdateWrapper = new LambdaUpdateWrapper<>();
            lambdaUpdateWrapper.eq(LabReqState::getId, array[i]);
            lambdaUpdateWrapper.set(LabReqState::getDelFlag, 1);
            boolean b = this.update(null, lambdaUpdateWrapper);
            if (!b) {
                return Result.fail("删除失败！");
            }
        }
        return Result.succ(1, "删除成功！", null);
    }

    //获取申请状态
    @Override
    public String getReqState(String reqMainId) {
        LambdaQueryWrapper<LabReqState> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(LabReqState::getDelFlag, 0).eq(LabReqState::getReqMainId, reqMainId).orderByDesc(LabReqState::getSeq);

        LabReqState labReqState = labReqStateMapper.selectOne(queryWrapper);
        return labReqState.getStateCode();
    }

    @Override
    public Long getMaxCount(String reqNo) {
        LambdaQueryWrapper<LabReqState> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(LabReqState::getDelFlag, 0).eq(LabReqState::getReqNo, reqNo);
        Integer i = labReqStateMapper.selectCount(queryWrapper);
        return ((Integer) (i + 1)).longValue();
    }


}
